package cn.inno.system;

import cn.inno.dao.SysAsideMapper;
import cn.inno.dao.SysBaseMapper;
import cn.inno.entitys.ErrorMsg;
import cn.inno.entitys.SysAside;
import cn.inno.entitys.SysAsideExample;
import cn.inno.entitys.SysBase;
import cn.inno.exceptions.BusinessException;
import cn.inno.service.BaseService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 系统配置服务类
 *
 * @author LeungZengJian
 */
@Service
public class SystemService extends BaseService {

    @Value("${spring.application.name}")
    private String sys_id;

    @Autowired
    private SysBaseMapper sysBaseMapper;
    @Autowired
    private SysAsideMapper sysAsideMapper;
    @Autowired
    private SystemDao systemDao;

    public JSONObject baseEdit() {

        SysBase sysBase = this.sysBaseMapper.selectByPrimaryKey(sys_id);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_DATA, sysBase);
        return this.executeRerutn(jsonObject);
    }

    public JSONObject baseUp(SysBase sysBase) {

        this.sysBaseMapper.updateByPrimaryKey(sysBase);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_DATA, sysBase);
        return this.executeRerutn(jsonObject);
    }

    public JSONObject categoryList(String adminId) {

        Map formMap = new HashMap();
        formMap.put("adminId", adminId);

        List lists = this.systemDao.categoryList(formMap);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_DATA, lists);
        return this.executeRerutn(jsonObject);
    }

    public JSONObject categoryAdd(SysAside sysAside, String adminId) {

        sysAside.setAddName(adminId);
        sysAside.setAddTime(new Date());
        this.sysAsideMapper.updateByPrimaryKeySelective(sysAside);

        JSONObject jsonObject = new JSONObject();
        return this.executeRerutn(jsonObject);
    }

    public JSONObject categoryUp(SysAside sysAside, String adminId) {

        sysAside.setUpName(adminId);
        sysAside.setUpTime(new Date());
        this.sysAsideMapper.updateByPrimaryKeySelective(sysAside);

        JSONObject jsonObject = new JSONObject();
        return this.executeRerutn(jsonObject);
    }

    public void categoryCheck(SysAside sysAside) {
        if (StringUtils.isBlank(sysAside.getTitle())) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "栏目名称不能为空");
        }

        SysAsideExample sysAsideExample = new SysAsideExample();
        sysAsideExample.createCriteria().andTitleEqualTo(sysAside.getTitle());

        Long rel = this.sysAsideMapper.countByExample(sysAsideExample);

        if (rel != 0) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "栏目名称已经被占用");
        }

    }

    public JSONObject categoryDel(String asideIds, String adminId) {

        List<String> result = Arrays.asList(asideIds.split(","));

        Map formMap = new HashMap();

        formMap.put("asideIds", result);

        this.systemDao.categoryDel(formMap);

        JSONObject jsonObject = new JSONObject();
        return executeRerutn(jsonObject);
    }

    public JSONObject categoryUpStatus(SysAside sysAside) {

        this.sysAsideMapper.updateByPrimaryKeySelective(sysAside);

        JSONObject jsonObject = new JSONObject();
        return this.executeRerutn(jsonObject);
    }

    public JSONObject categoryEdit(String asideId) {

        SysAside sysAside;
        if (StringUtils.isNotBlank(asideId)) {
            sysAside = this.sysAsideMapper.selectByPrimaryKey(asideId);
        } else {
            sysAside = new SysAside();
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_DATA, sysAside);
        jsonObject.put("asideSelect", this.categorySelect());
        return this.executeRerutn(jsonObject);
    }

    public List categorySelect() {
        SysAsideExample sysAsideExample = new SysAsideExample();
        sysAsideExample.createCriteria().andSupAsideIsNull();

        return this.sysAsideMapper.selectByExample(sysAsideExample);
    }

}
